//
//  HWhushouOtCViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/5/29.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWhushouOtCViewController.h"
//#import "HWMoWanExchangeTableViewCell.h"
#import "HWyaoqijiluTableViewCell.h"


@interface HWhushouOtCViewController ()<UITableViewDelegate,UITableViewDataSource>



@property(strong,nonatomic) UITableView *mainaTableVie;

@property(strong,nonatomic)  NSString *p;
@property(strong,nonatomic)  NSString *max_p;
@property(strong,nonatomic)  NSMutableArray *dataMuArr;



@end

@implementation HWhushouOtCViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0,0, kScreenW,kScreenH-44-CF_NavHeight-20) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
         _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
         _mainaTableVie.backgroundColor = RGB(248, 248, 248);

    }
    return   _mainaTableVie ;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    //设置UI
    
    [self setupUI];
    [self addrefreshControl];
    
    
    
}



// 设置UI
- (void)setupUI {
    
    
    [self.view addSubview:self.mainaTableVie];
    
    
}


#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}






// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"invite_record",
                                       @"uid" : uidStr,
                                       @"t":tStr,
                                       @"sign":singMD5,
                                       @"type" :typeStr,
                                       @"member_id":[HWLocallyStoredData getCode],
                                       
            
                                     };
    
    
    
       
       [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
             //   self.dataMuArr = [HWExchangeModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               
               self.dataMuArr =   [response[@"data"] mutableCopy];
               
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];

               [self.mainaTableVie reloadData];

    
           }
        
           
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return ;
         
        
      }
                   

      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
      NSString *typeStr =  [NSString stringWithFormat:@"%@",@"1"];
     

     NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];

     NSString *singMD5 = [WMUtil md5HexDigest:singStr];


    
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"newapp",
                                       @"ac" : @"invite_record",
                                       @"uid" : uidStr,
                                       @"t":tStr,
                                       @"sign":singMD5,
                                       @"type" :typeStr,
                                       @"member_id":[HWLocallyStoredData getCode],
                                       
            
                                     };
    
    
         
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                 NSMutableArray *mutableArr =   [response[@"data"] mutableCopy];


                   for (int i = 0; i < mutableArr.count; i++) {
                       [self.dataMuArr addObject:mutableArr[i]];

                   }

                  self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                  self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                  [self.mainaTableVie reloadData];
                 
        
              }
          
             
             
         } failure:^(NSString *error) {
             
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
            
         }];
      
    
}














- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
   
    
    return   self.dataMuArr.count;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *cellID = @"cellID";
     
//       HWMoWanExchangeTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//
//       if (!cell) {
//           cell =  [[NSBundle mainBundle]loadNibNamed:@"HWMoWanExchangeTableViewCell" owner:nil options:nil].firstObject;
//
//
//       }
//    //    cell.shujuModel = self.dataMuArr[indexPath.row];
//
//      cell.duihuanModel = self.dataMuArr[indexPath.row];
//
//       cell.selectionStyle  =  UITableViewCellSelectionStyleNone;
//
//
//      [cell.duihuanBut addTarget:self action:@selector(receiveButtap:) forControlEvents:UIControlEventTouchUpInside];
   HWyaoqijiluTableViewCell  *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
  
  if (!cell) {
      cell =  [[NSBundle mainBundle]loadNibNamed:@"HWyaoqijiluTableViewCell" owner:nil options:nil].firstObject;
      
  }
  
  NSDictionary *dic  =self.dataMuArr[indexPath.row];
    cell.name1label.text = [NSString stringWithFormat:@"%@",dic[@"user_name"]];
    
    cell.name2Label.text = [NSString stringWithFormat:@"%@",dic[@"points"]];
  
  cell.name3Label.text = [NSString stringWithFormat:@"%@",dic[@"time"]];
  
  cell.name4Label.text = [NSString stringWithFormat:@"%@",dic[@"note"]];
  
     return cell;
  
    
    
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    
    return  70;
    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    HWmaichuxiangqingViewController *shouchumainruView = [[HWmaichuxiangqingViewController alloc]init];
//      HWMyjiayiModel *model =   self.dataMuArr[indexPath.row];
//    shouchumainruView.idString = model.id;
//    [self.navigationController pushViewController:shouchumainruView animated:YES];
//
     
    
}


- (void)qurendingdaintap:(UIButton *)sender  {
    
    NSLog(@"%@",[[[sender superview] superview] superview]);
    
//
//    HWMyjiaoyiGouChuCell *cell = (HWMyjiaoyiGouChuCell *)[[[sender superview] superview] superview];
//
//    NSIndexPath  *indexPath = [self.mainaTableVie indexPathForCell:cell];
//
//    HWMyjiayiModel *model =   self.dataMuArr[indexPath.row];
//
//    [self maijiaquerenfukuan:model];
//
//
//
    
    
    
    
    

}


//买家确认付款

//- (void)maijiaquerenfukuan:(HWMyjiayiModel *)model{
//    
//    
//    
//    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
//     
//
//         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//    NSString *idStr = model.id;
//        
//      //  NSString *typeStr = @"0";
//
//          NSString *singStr = [NSString stringWithFormat:@"%@%@%@%@",uidStr,idStr,tStr,@"fe50340b1bc1d07e464e994a930281d6"];
//          NSString *singMD5 = [WMUtil md5HexDigest:singStr];
//          NSDictionary *parametes = @{
//
//                                                @"ios" : @"1",
//                                                @"ct" :  @"app",
//                                                @"ac":@"c51pay2",
//                                                @"uid": uidStr,
//                                                @"id":idStr,
//                                                @"t" : tStr,
//                                                @"sign" :singMD5,
//                                               };
//
//
//          [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//
//
//                   NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//
//
//                  [MBProgressHUD hideHUD];
//                   if ([codeStr intValue] == 1) {
//
//
//                       [MBProgressHUD showError:response[@"message"]];
//                   //    [self.navigationController  popViewControllerAnimated:YES];
//                       
//                       
//                    
//
//
//                   } else {
//
//                        [MBProgressHUD showError:response[@"message"]];
//
//
//                   }
//
//
//
//               } failure:^(NSString *error) {
//
//                   [MBProgressHUD hideHUD];
//
//               }];
//    
//    
//
//}

- (void)reshushuju  {
   
   if (self.mainaTableVie == nil) {
       
       
       
   } else{
       
       [self.mainaTableVie.mj_header beginRefreshing];
       
   }
   
}

// 点击领取按钮

//- (void)receiveButtap:(UIButton *)sender {
//    
//    NSLog(@"%@",[[[sender superview] superview] superview]);
//    
//    
//    
//    
//    HWMoWanExchangeTableViewCell *cell = (HWMoWanExchangeTableViewCell *)[[[sender superview] superview] superview];
//    NSIndexPath *index = [self.mainaTableVie indexPathForCell:cell];
//    
//     HWExchangeModel *mode  = self.dataMuArr[index.row];
//    
//    
//      [MBProgressHUD show:@"正在加载"];
//    
//     
//    
//       NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
//       NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//       
//       NSString *typeStr =  [NSString stringWithFormat:@"%@",@"2"];
//       
//    //   NSString *singStr = [NSString stringWithFormat:@"%@%@%@%@",uidStr,typeStr,tStr,@"fe50340b1bc1d07e464e994a930281d6"];
//       
//    //    NSString *singMD5 = [WMUtil md5HexDigest:singStr];
//
//       self.dataMuArr = [[NSMutableArray alloc]init];
//       NSDictionary *parametes = @{
//                                         
//                                          @"ios" : @"1",
//                                          @"ct" :  @"newapp",
//                                          @"ac" : @"getcoupon",
//                                          @"type" : typeStr,
//                                          @"uid" :uidStr,
//                                          @"id": mode.id
//                                
//                                        };
//       
//       
//       
//          
//          [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//              
//              // [SVProgressHUD dismiss];
//              
//              
//              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//              
//              if ([codeStr intValue] == 1) {
//                  
//                   
//                    [MBProgressHUD showError:response[@"message"]];
//                    [self.mainaTableVie.mj_header beginRefreshing];
//              
//       
//              } else {
//                  
//                  [MBProgressHUD showError:response[@"message"]];
//                  
//              }
//                  
//                  
//           
//             
//              
//              
//              
//          } failure:^(NSString *error) {
//              
//               [MBProgressHUD hideHUD];
//              [self.mainaTableVie.mj_header endRefreshing];
//              [self.mainaTableVie.mj_footer endRefreshing];
//              
//              
//          }];
//    
//    
//    
//    
//    
//    
//}



@end
